import Vue from 'vue'
import VueRouter from 'vue-router'
import AdminLogin from '@/components/AdminLogin.vue'
import Home from '@/components/Home.vue'
import Welcome from '@/components/Welcome.vue'
import TicketView from '@/components/Ticket/TicketView.vue'
import TicketAdd from '@/components/Ticket/TicketAdd.vue'
import OrderProcess from '@/components/Order/OrderProcess.vue'
import OrderView from '@/components/Order/OrderView.vue' 
import LivePublish from '@/components/Live/LivePublish.vue'
import Information from '@/components/Information/Information.vue'
import User from '@/components/User.vue'
import MessageAdd from '@/components/Message/MessageAdd.vue'
import ArtistAdd from '@/components/Artist/ArtistAdd.vue'
import ArtistView from '@/components/Artist/ArtistView.vue'
import personalview from '@/components/personal/personalview.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: AdminLogin
  },
  {
    path: '/',
    name: 'home',
    component: Home,
    children: [
      {
        path: 'welcome',
        component: Welcome
      },
      {
        path: 'Live/LivePublish',
        name: 'LivePublish',
        component: LivePublish,
      },
      {
        path: 'tickets/view',
        name: 'TicketView',
        component: TicketView,
      },
      {
        path: 'tickets/add',
        name: 'TicketAdd',
        component: TicketAdd,
      },
      {
        path: 'orders/process',
        name: 'OrderProcess',
        component: OrderProcess,
      },
      {
        path: 'orders/view',
        name: 'OrderView',
        component: OrderView,
      },
      {
        path: 'Information/Information',
        name: 'Information',
        component: Information,
      },
      {
        path: 'User',
        component: User,
      },
      {
        path: 'Artist/ArtistAdd',
        name: 'ArtistAdd',
        component: ArtistAdd,
      },
      {
        path: 'Artist/ArtistView',
        name: 'ArtistView',
        component: ArtistView,
      },
      {
        path: 'Message/MessageAdd',
        name: 'MessageAdd',
        component: MessageAdd,
      },
      {
        path: 'personal/personalview',
        name: 'personalview',
        component: personalview,
      },
    ],
    meta: { requiresAuth: true } // 设置需要身份验证
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  // 检查是否需要身份验证
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // 这里假设你有一个方法来检查用户是否已登录
    // 如果用户已经登录，则继续导航
    // 否则，重定向到登录页面，并保存要访问的路径
    if (!isAuthenticated()) {
      next({
        path: '/login',
        query: { redirect: to.fullPath } // 保存用户想要访问的地址
      });
    } else {
      next();
    }
  } else {
    // 对于不需要身份验证的路由，直接导航
    next();
  }
});

// 检查用户是否已登录的函数
function isAuthenticated() {
  // 这里根据你的逻辑来判断用户是否已登录，比如判断 localStorage 中是否有登录信息等
  return localStorage.getItem('currentUser') !== null;
}

export default router
